<template>
  <div v-show="option === 'reset'" class="container">
    <el-form ref="formRef" :model="form" :rules="rules" class="form-bar" label-width="120px">
      <el-form-item prop="email">
        <el-input
            v-model="form.email"
            placeholder="请输入邮箱"
            prefix-icon="Message"
            size="large"
        />
      </el-form-item>
      <el-form-item prop="code">
        <el-input
            v-model="form.code"
            placeholder="请输入验证码"
            show-password
            size="large"
        >
          <template #append>
            <span
                v-if="mobileCodeTimer <= 0"
                class="getMobileCode"
                style="cursor: pointer"
                @click="getEmailCode"
            >
                    获取验证码
                  </span>
            <span v-if="mobileCodeTimer > 0" class="getMobileCode" style="cursor: pointer">
                    {{ mobileCodeTimer }}秒后可重新获取
                  </span>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item v-if="showPassword" prop="password">
        <el-input
            v-model="form.password"
            placeholder="请输入新密码"
            prefix-icon="Lock"
            show-password
            size="large"
            type="password"
            @keyup.enter="handleReset()"
        />
      </el-form-item>
      <el-form-item>
        <el-button
            :loading="loading"
            class="full-bar"
            size="large"
            type="primary"
            @click="handleReset()"
        >确定
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button
            class="full-bar"
            size="large"
            @click="option = 'login'"
        >返回登录
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script name="LoginReset" setup>
import {getCurrentInstance, reactive, ref, toRefs} from "vue";
import {forgetPassword, sendEmailCode} from "@/api/login";

const {proxy} = getCurrentInstance();

const option = defineModel()
const loading = ref(false);
const mobileCodeTimer = ref(0)
const showPassword = ref(false)
const formRef = ref()

const data = reactive({
  form: {
    email: null,
    code: null,
    password: null,
    tenantName: '芋道源码',
  },
  rules: {
    email: [
      {required: true, message: "邮箱不能为空", trigger: ["blur", "change"]}
    ],
    code: [
      {required: true, message: "验证码不能为空", trigger: ["blur", "change"]}
    ],
    password: [
      {required: true, message: "密码不能为空", trigger: ["blur", "change"]}
    ],
  }
});

const {form, rules} = toRefs(data);

const getEmailCode = async () => {
  formRef.value.validateField('email', async (valid) => {
    if (valid) {
      await sendEmailCode(form.value).then(async () => {
        proxy.$modal.msgSuccess('消息已发送')
        showPassword.value = true
        // 设置倒计时
        mobileCodeTimer.value = 60
        let msgTimer = setInterval(() => {
          mobileCodeTimer.value = mobileCodeTimer.value - 1
          if (mobileCodeTimer.value <= 0) {
            clearInterval(msgTimer)
          }
        }, 1000)
      })
    }
  });
}

function handleReset() {
  forgetPassword(form.value).then(res => {
    if (res.data) {
      proxy.$modal.msgSuccess('密码重置成功，请登录')
      option.value = 'login'
    }
  })
}

</script>

<style lang="scss" scoped>
.container {
  height: calc(100% - 60px);
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;

  .form-bar {
    width: 500px;

    :deep(.el-input-group__append) {
      background-color: #409eff;
      color: #ffffff;
      font-weight: bold;
    }
  }
}
</style>